package pers.lmd.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pers.lmd.domain.R;
import pers.lmd.domain.dto.ArticleDTO;
import pers.lmd.common.constant.ResultEnum;
import pers.lmd.service.IArticleService;
import pers.lmd.common.utils.FileCustomUtil;
import pers.lmd.common.utils.ResultUtil;

import java.io.IOException;

/**
 * @Author lmd
 * @Date 2023/5/6 6:18
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system")
@Tag(name = "文章控制器")
public class ArticleController {
    private final FileCustomUtil fileCustomUtil;
    private final IArticleService IArticleService;

    @Parameters({
            @Parameter(name = "Authorization", description = "携带请求 token ", required = true, in = ParameterIn.HEADER),
            @Parameter(name = "title", description = "文章标题", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "content", description = "文章内容", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "articleDTO", hidden = true)
    })
    @Operation(summary = "添加文章")
    @PostMapping("/article")
    public R<Void> addArticle(ArticleDTO articleDTO, @RequestParam("file") MultipartFile articleImageFile) throws IOException {
        String articleImage = fileCustomUtil.uploadArticle(articleImageFile);
        IArticleService.addArticle(articleImage, articleDTO);
        return ResultUtil.success(ResultEnum.SUCCESS);
    }
}
